<?php  
	namespace Home\Controller;
	use Think\Controller;
	//专业管理
	//@author  程传民
	class MajorController extends AllowController{
		
		public function index(){
			$mod = D("major");
			$tot = $mod->count();
			$list = $mod->select();
			$page = new \Think\Page($tot,5);

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
			
	    	$list=  $mod->limit($page->firstRow,$page->listRows)->select();
	    	foreach($list as $key => $value){
	    		if($list[$key]['addtime']==""){
	    			$list[$key]['addtime']="";
	    		}else{
	    			$list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
	    			
	    		}
	    	}
	    	// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("Major/major");
		}

		public function edit(){
			$mod = D("Major");
			$arr = array("id"=>I("get.id"));
			$id = I("get.id");
			$info = $mod->where($arr)->find();
			$list = array();
			$i=0;
			foreach($info as $k=>$v){
				if($k!="id" && $k!="addtime"){
					$list[$i][] = $k;
					$list[$i][] = $v;
					$i++;
				}
			}
			// var_dump($list);
			// exit;
			$this->assign("list",$list);
			$this->assign("id",$id);
			$this->display("Major/update");
		}

		public function update(){
			$arr = array("id"=>I("post.id"));
			// var_dump($_POST);
			// exit;
			// $data = $_POST;
			$major = D("major");
			$info = $major->where($arr)->save($_POST);
			if($info){
				$this->success("修改成功",U("Major/index"));
			}else{
				$this->error("修改失败");
			}
		}

		//如果当前专业下面还有班级就不能删除
		public function delete(){
			$arr = array("id"=>I("post.id"));
			$major = D("major");

			$class = D("class");

			$info = $class->where(array("major_id"=>I("post.id")))->find();

			if($info){
				echo "失败";
			}else{
				$major->where($arr)->delete();
				echo "成功";
			}
		}

		//添加专业
		public function add(){
			$this->display("Major/add");
		}

		public function insert(){
			$mod = D("Major");
			$data['name'] = I("post.majorname");
			$data['question1'] = I("post.question1");
			$data['question2'] = I("post.question2");
			$data['question3'] = I("post.question3");
			$data['question4'] = I("post.question4");
			$data['addtime'] = date(time());
			$info = $mod->add($data);
			if($info){
				echo "添加成功";
			}else{
				echo "失败";
			}
		}


		//为已建专业添加字段
		public function addfield(){
			$major = D("major");
			$list = $major->where(array("id"=>$_GET['id']))->find();
			$this->assign("list",$list);
			$this->display("Major/addfield");
		}

		public function doaddfield(){
			M()->query("alter table stu_major add {$_POST['question']} varchar(255) null;");
			M()->query("alter table stu_desire add {$_POST['question']} varchar(255) null;");
			echo "成功";
		}

	}

?>